<script lang="ts" setup>
import { ref } from 'vue';
import type { FormInstance } from '@arco-design/web-vue/es/form';
import { $fetch } from '@/settings/utils/fetch';
import useNotification from '@/settings/hooks/notification';
import { useI18n } from 'vue-i18n';

const props = defineProps({
  id: {
    type: String,
    default: '',
  },
  name: {
    type: String,
    default: '',
  },
});

const loading = ref(false);
const formData = ref({
  msg: '',
});
const formRef = ref<FormInstance>();
const { success } = useNotification();
const { t } = useI18n();

const dialogClickButton = async (button: string) => {
  if (button !== 'ok') {
    return;
  }

  const res = await formRef.value?.validate();

  if (res) {
    return false;
  }

  const { isSuccess } = await $fetch('/admin/warehouse/adminWarehouseCommunity', {
    method: 'post',
    data: {
      id: props.id,
      type: '2',
      ...formData.value,
    },
  });

  if (isSuccess) {
    success(t('settings.notification.success'));
  }

  return isSuccess;
}

defineExpose({
  dialogClickButton,
});
</script>

<template>
  <AForm ref="formRef" auto-label-width scroll-to-first-error :model="formData">
    <ASpin :loading="loading">
      <AFormItem :label="$t('settings.view.repository.rejectWindow.column')">
        {{ props.name }}
      </AFormItem>

      <AFormItem field="msg" :label="$t('settings.view.repository.rejectWindow.refuse')">
        <ATextarea v-model="formData.msg" allow-clear show-word-limit />
      </AFormItem>
    </ASpin>
  </AForm>
</template>

<style lang="scss" scoped>
</style>
